// pages/me/pages/usersinfo/usersinfo.js
const app=getApp();
var util = require('../../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickname: '',
    telephone:'',
    sexShow:false,
    sex:0,
    isBoy:true,
    isGirl:false,
    isBm:false,
    sexText:'',
    imgSrc:'../../../../images/user_img.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfoFun();
  },
  getUserInfoFun:function(){
    var that = this;
    var myDate = new Date();
    var timestamp = new Date().getTime();
    console.log(app.globalData.openKey);
    wx.request({
      url: 'https://papi.zcyun.cn/v1/user/',
      data: {},
      method: "GET",
      header: {
        'content-type': 'application/json', // 默认值
        'openId': app.globalData.openId,
        'ts': timestamp,
        'sign': util.sha1("GET" + '/v1/user/' + timestamp + app.globalData.openKey)
      },
      success: function (res) {
        console.log(res);
        if (res.data.data.nickname == undefined) {
          that.setData({
            nickname: "",
          })
        } else {
          that.setData({
            nickname: res.data.data.nickname,
          })
          app.globalData.nickname = res.data.data.nickname;
        }
        if (res.data.data.avatar == undefined) {
          that.setData({
            imgSrc: "../../../../images/user_img.png",
          })
        } else {
          that.setData({
            imgSrc: res.data.data.avatar,
          })
        }
        that.setData({
          telephone: res.data.data.telephone
        })
        if (res.data.data.sex == 0) {
          that.setData({
            sexText: "男",
            sex: 0
          })
        } else if (res.data.data.sex == 1) {
          that.setData({
            sexText: "女",
            sex: 1
          })
        } else {
          that.setData({
            sexText: "保密",
            sex: 2
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
   
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  sexFun: function () {
    var that=this;
    var params = {
      "sex": that.data.sex
    }
    var myDate = new Date();
    var timestamp = new Date().getTime();
    wx.request({
      url: 'https://papi.zcyun.cn/v1/user/',
      data: {
        "sex": that.data.sex
      },
      method: "PUT",
      header: {
        'content-type': 'application/json', // 默认值
        'openId': app.globalData.openId,
        'ts': timestamp,
        'sign': util.sha1("PUT" + '/v1/user/' + JSON.stringify(params) + timestamp + app.globalData.openKey)
      },
      success: function (res) {
        console.log(res);
        if(that.data.sex==0){
          that.setData({
            sexText:"男"
          })
        }else if(that.data.sex==1){
          that.setData({
            sexText: "女"
          })
        }else{
          that.setData({
            sexText: "保密"
          })
        }
      }
    })
  },
  actioncnt:function(){
    if(this.data.sex==0){
      this.setData({
        isBoy: true,
        isGirl: false,
        isBm: false,
      })
    } else if (this.data.sex == 1){
      this.setData({
        isBoy: false,
        isGirl: true,
        isBm: false,
      })
    }else{
      this.setData({
        isBoy: false,
        isGirl: false,
        isBm: true,
      })
    }
    this.setData({
      sexShow: true
    })
  },
  boySubmit: function () {
    this.setData({
      isBoy: true,
      isGirl: false,
      isBm: false,
      sexShow: false,
      sex:0
    })
    this.sexFun();
  },
  girlSubmit: function () {
    this.setData({
      isBoy: false,
      isGirl: true,
      isBm: false,
      sexShow: false,
      sex: 1
    })
    this.sexFun();
  },
  bmSubmit: function () {
    this.setData({
      isBoy: false,
      isGirl: false,
      isBm: true,
      sexShow: false,
      sex: 2
    })
    this.sexFun();
  },
  // 修改昵称
  nickSubmit:function(){
    wx.navigateTo({
      url: '../nickname/nickname',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  // 修改密码
  modifyBind:function(){
    wx.navigateTo({
      url: '../../modifyPwd/modifyPwd',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 退出账号
  quitBtnBind:function(){
    var myDate = new Date();
    var timestamp = new Date().getTime();
    wx.request({
      url: 'https://papi.zcyun.cn/v1/user/logout',
      data: {
      },
      method: "GET",
      header: {
        'content-type': 'application/json', // 默认值
        'openId': app.globalData.openId,
        'ts': timestamp,
        'sign': util.sha1("GET" + '/v1/user/logout' + timestamp + app.globalData.openKey)
      },
      success: function (res) {
        console.log(res);
        if(res.data.code==0){
          app.globalData.isLogin=false;
          app.globalData.openKey='';
          app.globalData.openId='';
          wx.removeStorage({
            key: 'openKey',
            success(res) {
              console.log(res)
            }
          })
          wx.removeStorage({
            key: 'openId',
            success(res) {
              console.log(res)
            }
          })
          wx.removeStorage({
            key: 'isLogin',
            success(res) {
              console.log(res)
            }
          })
          wx.switchTab({
            url: '../../index/index'
          })
        }
      }
    })
  },
  // 更换手机号跳转
  replaceBind:function(){
    wx.navigateTo({
      url: '../userRegister/userRegister?telephone=' + this.data.telephone + '&isRegister=2',
    })
  },
  // 选择头像
  selectImageBind:function(){
    var that=this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#3486ff",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  chooseWxImage: function (type){
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success(res) {
        console.log(res);
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePath = res.tempFilePaths["0"];
        var imageType = "png";
        wx.getImageInfo({
          src: res.tempFilePaths[0],
          success(res) {
            imageType = res.type;
            console.log(imageType)
          }
        })
        wx.navigateTo({
          url: "../../../capHeadImage/capHeadImage?src=" + tempFilePath,
        })
        // const imageName = tempFilePath.split("/");
        // console.log(imageName);
        // console.log(imageName[imageName.length - 1])
        // var baseCode = "";
        // // 图片转base64
        // wx.getFileSystemManager().readFile({
        //   filePath: tempFilePath, //选择图片返回的相对路径
        //   encoding: 'base64', //编码格式
        //   success: res => { //成功的回调
        //     console.log('data:image/' + imageType + ';base64,' + res.data)
        //     baseCode = res.data;
        //   }
        // })
        // var myDate = new Date();
        // var timestamp = new Date().getTime();
        // var param = {
        //   "base64": 'data:image/' + imageType + ';base64,' + baseCode,
        //   "filename": imageName[imageName.length - 1]
        // }
        // var params = JSON.stringify(param);
        // wx.request({
        //   url: 'https://papi.zcyun.cn/v1/user/avatar',
        //   data: {
        //     "base64": 'data:image/' + imageType + ';base64,' + baseCode,
        //     "filename": imageName[imageName.length - 1]
        //   },
        //   method: "PUT",
        //   header: {
        //     'content-type': 'application/json', // 默认值
        //     'openId': app.globalData.openId,
        //     'ts': timestamp,
        //     'sign': util.sha1("PUT" + '/v1/user/avatar' + params + timestamp + app.globalData.openKey)
        //   },
        //   success: function (res) {
        //     console.log(res.data);
        //     if (res.data.code == 0) {
        //       that.setData({
        //         imgSrc: res.data.avatarPath,
        //       })
        //     }
        //   }
        // })
      }
    })
  }

})